package com.njrsrc.android.app;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.njrsrc.R;
import com.njrsrc.android.bean.IndustryInfo;
import com.njrsrc.android.library.vpi.BaseSampleActivity;
import com.njrsrc.android.views.TitleBar;

/**
 * 选择行业列表页面
 * 
 * @author Administrator
 * 
 */
public class SelectIndustryActivity extends BaseSampleActivity implements OnClickListener {
	private ListView industryListView;
	private IndustryAdapter adapter;
	private TitleBar mBar;
	private TextView numberTextView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_industry);
	}

	@Override
	protected void onResume() {
		initView();
		super.onResume();
	}

	private void initView() {
		mBar = (TitleBar) findViewById(R.id.activity_title_bar);
		mBar.setTitleText("选择行业");
		mBar.back.setOnClickListener(this);
		mBar.setRightBg(R.drawable.find_job_sub_title_tight);
		mBar.right.setOnClickListener(this);

		numberTextView = (TextView) findViewById(R.id.topic_number);
		industryListView = (ListView) findViewById(R.id.select_industry_listView);
		adapter = new IndustryAdapter(SelectIndustryActivity.this, AppApplication.industryList);
		industryListView.setAdapter(adapter);
		try {
			updateTopic(null);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}
	}

	private void updateTopic(RelativeLayout layout) throws IllegalArgumentException, IllegalAccessException,
			NoSuchFieldException {
		IndustryInfo industryInfo = null;
		if(layout != null)
		{
			industryInfo = (IndustryInfo) layout.getTag(R.id.industry_info);
		}
		// 更新数字
		int number = AppApplication.chooseIndustry.size();
		if (number == 0) {
			numberTextView.setVisibility(View.GONE);
		} else {
			numberTextView.setVisibility(View.VISIBLE);
			numberTextView.setText(number + "/3");
		}
		if (industryInfo == null) {
			int size = AppApplication.chooseIndustry.size();
			if(size == 0)
			{
				findViewById(R.id.topic_tv01).setVisibility(View.GONE);
				findViewById(R.id.topic_tv02).setVisibility(View.GONE);
				findViewById(R.id.topic_tv03).setVisibility(View.GONE);
			}else
			{
				for (int i = 0; i < size; i++) {
					((TextView) findViewById(R.id.class.getDeclaredField("topic_tv0" + (i + 1)).getInt(this)))
							.setText(AppApplication.chooseIndustry.get(i).getName());
					((TextView) findViewById(R.id.class.getDeclaredField("topic_tv0" + (i + 1)).getInt(this)))
							.setVisibility(View.VISIBLE);
					((TextView) findViewById(R.id.class.getDeclaredField("topic_tv0" + (i + 1)).getInt(this)))
							.setBackgroundResource(R.drawable.chat_message_bg);
					((TextView) findViewById(R.id.class.getDeclaredField("topic_tv0" + (i + 1)).getInt(this)))
					.setTag(AppApplication.chooseIndustry.get(i));
					((TextView) findViewById(R.id.class.getDeclaredField("topic_tv0" + (i + 1)).getInt(this)))
					.setOnClickListener(this);
				}
			}
			return;
		}
		if (!AppApplication.chooseIndustry.contains(industryInfo)) {
			if (AppApplication.chooseIndustry.size() == 3) {
				return;
			}
			AppApplication.chooseIndustry.add(industryInfo);
//			int size = AppApplication.chooseIndustry.size();
			for (int index = 0; index < 3; index++) {
				TextView tv = ((TextView) findViewById(R.id.class.getDeclaredField("topic_tv0" + (index + 1)).getInt(this)));
				if(tv.getVisibility() == View.GONE)
				{
					((TextView) findViewById(R.id.class.getDeclaredField("topic_tv0" + (index + 1)).getInt(this)))
					.setText(industryInfo.getName());
					((TextView) findViewById(R.id.class.getDeclaredField("topic_tv0" + (index + 1)).getInt(this)))
							.setVisibility(View.VISIBLE);
					((TextView) findViewById(R.id.class.getDeclaredField("topic_tv0" + (index + 1)).getInt(this)))
							.setBackgroundResource(R.drawable.chat_message_bg);
					((TextView) findViewById(R.id.class.getDeclaredField("topic_tv0" + (index + 1)).getInt(this)))
					.setTag(layout);
					((TextView) findViewById(R.id.class.getDeclaredField("topic_tv0" + (index + 1)).getInt(this)))
					.setOnClickListener(this);
					break;
				}
			}
		} else {
			cancelTextViewSelected(industryInfo);
		}
		// 更新数字
		int number2 = AppApplication.chooseIndustry.size();
		if (number2 == 0) {
			numberTextView.setVisibility(View.GONE);
		} else {
			numberTextView.setVisibility(View.VISIBLE);
			numberTextView.setText(number2 + "/3");
		}
	}

	public class IndustryAdapter extends BaseAdapter {
		private List<IndustryInfo> list;
		private Context mContext;

		public IndustryAdapter(Context mContext, List<IndustryInfo> list) {
			this.mContext = mContext;
			this.list = list;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			return list.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup arg2) {
			final Holder holder;
			if (convertView == null) {
				holder = new Holder();
				LayoutInflater inflater = LayoutInflater.from(mContext);
				convertView = inflater.inflate(R.layout.item_list_industry, null);
				holder.layout = (RelativeLayout) convertView.findViewById(R.id.item_list_industry_layout);
				holder.name = (TextView) convertView.findViewById(R.id.item_list_industry_tv);
				convertView.setTag(holder);
			} else {
				holder = (Holder) convertView.getTag();
			}
			holder.name.setText(list.get(position).getName());

			if (AppApplication.industryList.get(position).getCode().equals(list.get(position).getCode())
					&& AppApplication.industryList.get(position).isSelected()) {
				holder.layout.setBackgroundResource(R.drawable.public_item_bg_press);
			} else {
				holder.layout.setBackgroundResource(R.drawable.public_item_selector);
			}

			OnClickListener listener = new OnClickListener() {

				@Override
				public void onClick(View v) {
					int number = AppApplication.chooseIndustry.size();
					if (AppApplication.industryList.get(position).getCode().equals(list.get(position).getCode())
							&& !AppApplication.industryList.get(position).isSelected()) {
						if (number >= 3) {
							return;
						}
						holder.layout.setBackgroundResource(R.drawable.public_item_bg_press);
						AppApplication.industryList.get(position).setSelected(true);
					} else {
						holder.layout.setBackgroundResource(R.drawable.public_item_selector);
						AppApplication.industryList.get(position).setSelected(false);
					}

					IndustryInfo industryInfo = new IndustryInfo(AppApplication.industryList.get(position).getName(),
							AppApplication.industryList.get(position).getCode());
					holder.layout.setTag(R.id.industry_info,industryInfo);
					try {
						updateTopic(holder.layout);
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					} catch (NoSuchFieldException e) {
						e.printStackTrace();
					}
				}
			};
			holder.layout.setOnClickListener(listener);
			return convertView;
		}
	}

	class Holder {
		RelativeLayout layout;
		TextView name;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tb_ibtn_back:
			// AppApplication.clear();
			setResult(0);
			finish();
			break;
		case R.id.tb_ibtn_right:
			setResult(1);
			finish();
			break;
			
		case R.id.topic_tv01:
			IndustryInfo industryInfo01 = null;
			if(v.getTag() != null && v.getTag() instanceof RelativeLayout)
			{
				RelativeLayout layout01 = (RelativeLayout) v.getTag();
				layout01.setBackgroundResource(R.drawable.public_item_selector);
				if(layout01 != null && layout01.getTag(R.id.industry_info) != null && layout01.getTag(R.id.industry_info) instanceof IndustryInfo)
				{
					industryInfo01 = (IndustryInfo) layout01.getTag(R.id.industry_info);
				}
			}else if(v.getTag() != null && v.getTag() instanceof IndustryInfo)
			{
				industryInfo01 = (IndustryInfo) v.getTag();
			}
			if(industryInfo01 != null)
			{
				cancelTextViewSelected(industryInfo01);
			}
			break;
		case R.id.topic_tv02:
			IndustryInfo industryInfo02 = null;
			if(v.getTag() != null && v.getTag() instanceof RelativeLayout)
			{
				RelativeLayout layout02= (RelativeLayout) v.getTag();
				layout02.setBackgroundResource(R.drawable.public_item_selector);
				if(layout02 != null && layout02.getTag(R.id.industry_info) != null && layout02.getTag(R.id.industry_info) instanceof IndustryInfo)
				{
					industryInfo02 = (IndustryInfo) layout02.getTag(R.id.industry_info);
				}
			}else if(v.getTag() != null && v.getTag() instanceof IndustryInfo)
			{
				industryInfo02 = (IndustryInfo) v.getTag();
			}
			if(industryInfo02 != null)
			{
				cancelTextViewSelected(industryInfo02);
			}
			break;
		case R.id.topic_tv03:
			IndustryInfo industryInfo03 = null;
			if(v.getTag() != null && v.getTag() instanceof RelativeLayout)
			{
				RelativeLayout layout03= (RelativeLayout) v.getTag();
				layout03.setBackgroundResource(R.drawable.public_item_selector);
				if(layout03 != null && layout03.getTag(R.id.industry_info) != null && layout03.getTag(R.id.industry_info) instanceof IndustryInfo)
				{
					industryInfo03 = (IndustryInfo) layout03.getTag(R.id.industry_info);
				}
			}else if(v.getTag() != null && v.getTag() instanceof IndustryInfo)
			{
				industryInfo03 = (IndustryInfo) v.getTag();
			}
			if(industryInfo03 != null)
			{
				cancelTextViewSelected(industryInfo03);
			}
			break;
		default:
			break;
		}
	}
	
	private void cancelTextViewSelected(IndustryInfo industryInfo)
	{
		int index = AppApplication.chooseIndustry.indexOf(industryInfo);
		TextView tv1 = (TextView) findViewById(R.id.topic_tv01);
		TextView tv2 = (TextView) findViewById(R.id.topic_tv02);
		TextView tv3 = (TextView) findViewById(R.id.topic_tv03);
		String title1Name = tv1.getText().toString();
		String title2Name = tv2.getText().toString();
		String title3Name = tv3.getText().toString();
		
		if(industryInfo.getName().equals(title1Name))
		{
			tv1.setText("");
			tv1.setVisibility(View.GONE);
			tv1.setBackgroundDrawable(null);
		}else if(industryInfo.getName().equals(title2Name))
		{
			tv2.setText("");
			tv2.setVisibility(View.GONE);
			tv2.setBackgroundDrawable(null);
		}else if(industryInfo.getName().equals(title3Name))
		{
			tv3.setText("");
			tv3.setVisibility(View.GONE);
			tv3.setBackgroundDrawable(null);
		}
		AppApplication.chooseIndustry.remove(index);
		
		// 更新数字
		int number = AppApplication.chooseIndustry.size();
		if (number == 0) {
			numberTextView.setVisibility(View.GONE);
		} else {
			numberTextView.setVisibility(View.VISIBLE);
			numberTextView.setText(number + "/3");
				}
		
		for(int i= 0;i < AppApplication.industryList.size();i++)
		{
			IndustryInfo info = AppApplication.industryList.get(i);
			if(info.getCode().equals(industryInfo.getCode()))
			{
				info.setSelected(false);
			}
		}
	}
}
